﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WROA.Models.Entitys;
using WROA.Models.Services;
using WROA.Models.ViewModels;
using WROA.Models.IServices;

namespace WROA.Controllers
{
    public class FlowController : Controller
    {
        public static Dictionary<string, List<object>> flowDictionary = new Dictionary<string, List<object>>();//用于存放流程和步骤
        public static Dictionary<string, FormData> formDictionary = new Dictionary<string, FormData>();//用于设计表单时保存Flow对象的Oid
        Flow flowBind = new Flow();
        Procedure procedureBind = new Procedure();

        /// <summary>
        /// 新建流程
        /// </summary>
        public ActionResult NewFlow()
        {
            IFlowService fs = new FlowService();
            IList<SelectListItem> flowTypeList = fs.GetAllFlowTypeList();
            IList<SelectListItem> FormNameList = fs.GetAllFormNameList();
            NewFlowView model = new NewFlowView(flowBind, flowTypeList, FormNameList);
            return View(model);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult NewFlow(FormCollection form)
        {
            IUserService us = new UserService();
            IFlowService fs = new FlowService();

            foreach (KeyValuePair<string, List<object>> pair in flowDictionary)//查看字典是否存在用户名数据
            {
                if (pair.Key == "陈大捷")//存在用户名
                {
                    //重新保存页面数据到字典中
                    Flow updateFlow = new Flow();
                    updateFlow.User = us.GetUser("陈大捷");
                    updateFlow.Flowtype = fs.GetFlowType(form["FlowType"]);
                    updateFlow.Fl_name = form["FlowName"];
                    updateFlow.Fl_remark = form["FlowRemark"];
                    updateFlow.Fl_date = DateTime.Now;
                    updateFlow.Form = fs.GetForm(form["FormName"]);
                    updateFlow.Fl_isban = "启用";
                    pair.Value[0] = updateFlow;//0为Flow对象

                    if (pair.Value.Count == 1)//字典值只存在一条数据，步骤还没有，则进入空白步骤页
                    {
                        return RedirectToAction("FlowProcedure", "Flow");
                    }
                    else//存在步骤，显示第一个步骤的数据
                    {
                        int step = 1;//当前步骤
                        ViewData["Step"] = step;//绑定步骤

                        List<SelectListItem> typeList = new List<SelectListItem>();//下拉列表数据
                        typeList.Add(new SelectListItem() { Text = "人员", Value = "人员" });
                        typeList.Add(new SelectListItem() { Text = "职位", Value = "职位" });
                        FlowProcedureView model = new FlowProcedureView(procedureBind, typeList);
                        model.procedure = (Procedure)pair.Value[1];
                        foreach (SelectListItem sli in typeList)
                        {
                            if (model.procedure.Pr_type == sli.Text)//绑定相应的选定的下拉列表
                            {
                                sli.Selected = true;
                            }
                        }
                        model.typeList = typeList;
                        return View("FlowProcedure", model);
                    }
                }
            }

            //字典不存在用户名，则直接添加Flow对象
            //保存Flow对象到Dictionary

            Flow flow = new Flow();
            //flow.User = us.GetUser(User.Identity.Name);
            flow.User = us.GetUser("陈大捷");
            flow.Flowtype = fs.GetFlowType(form["FlowType"]);
            flow.Fl_name = form["FlowName"];
            flow.Fl_remark = form["FlowRemark"];
            flow.Fl_date = DateTime.Now;
            flow.Form = fs.GetForm(form["FormName"]);
            flow.Fl_isban = "启用";

            List<object> list = new List<object>();
            list.Add(flow);
            //flowDictionary.Add(User.Identity.Name, list);
            flowDictionary.Add("陈大捷", list);

            return RedirectToAction("FlowProcedure", "Flow");
        }

        /// <summary>
        /// 流程步骤
        /// </summary>
        public ActionResult FlowProcedure()
        {
            int step = 1;//当前步骤
            ViewData["Step"] = step;
            List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem() { Text = "人员", Value = "人员" });
            list.Add(new SelectListItem() { Text = "职位", Value = "职位" });
            FlowProcedureView model = new FlowProcedureView(procedureBind, list);
            return View(model);
        }

        /// <summary>
        /// 进入步骤页面、下一步Post
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult NextFlowProcedure(FormCollection form)
        {
            List<SelectListItem> typeList = new List<SelectListItem>();
            typeList.Add(new SelectListItem() { Text = "人员", Value = "人员" });
            typeList.Add(new SelectListItem() { Text = "职位", Value = "职位" });
            FlowProcedureView model = new FlowProcedureView(procedureBind, typeList);

            ViewData["Step"] = Convert.ToInt32(form["Step"]) + 1;//步骤加1

            foreach (KeyValuePair<string, List<object>> pair in flowDictionary)//判断下一步数据是否存在字典中
            {
                if (pair.Key == "陈大捷")
                {
                    if (pair.Value.Count > Convert.ToInt32(form["Step"]) + 1)//如果下一步数据已存在，则显示数据
                    {
                        //更新数据
                        Procedure updateProcedure = new Procedure();
                        updateProcedure.Step_id = Convert.ToInt32(form["Step"]);
                        updateProcedure.Pr_name = form["ProcedureName"];
                        updateProcedure.Pr_remark = form["ProcedureRemark"];
                        updateProcedure.Pr_type = form["ProcedureType"];
                        updateProcedure.Pr_typevalue = form["ProcedureTypeValue"];
                        updateProcedure.Pr_isfinished = false;
                        pair.Value[Convert.ToInt32(form["Step"])] = updateProcedure;

                        procedureBind = (Procedure)pair.Value[Convert.ToInt32(form["Step"]) + 1];//查找下一步骤数据
                        foreach (SelectListItem sli in typeList)
                        {
                            if (procedureBind.Pr_type == sli.Text)//绑定相应的选定的下拉列表
                            {
                                sli.Selected = true;
                            }
                        }
                        model.typeList = typeList;
                        model.procedure = procedureBind;
                        return View("FlowProcedure", model);
                    }
                }
            }

            ////字典不存在下一步，则直接添加Procedure对象
            IFlowService fs = new FlowService();

            Procedure procedure = new Procedure();
            foreach (KeyValuePair<string, List<object>> pair in flowDictionary)
            {
                //if (pair.Key == User.Identity.Name)
                if (pair.Key == "陈大捷")
                {
                    procedure.Flow = (Flow)pair.Value[0];//获取步骤类型
                }
            }
            procedure.Step_id = Convert.ToInt32(form["Step"]);
            procedure.Pr_name = form["ProcedureName"];
            procedure.Pr_remark = form["ProcedureRemark"];
            procedure.Pr_type = form["ProcedureType"];
            procedure.Pr_typevalue = form["ProcedureTypeValue"];
            procedure.Pr_isfinished = false;

            //遍历dictionary，查找key为登录名的dictionary，添加对象
            foreach (KeyValuePair<string, List<object>> pair in flowDictionary)
            {
                //if (pair.Key == User.Identity.Name)
                if (pair.Key == "陈大捷")
                {
                    pair.Value.Add(procedure);
                }
            }
            return View("FlowProcedure", model);
        }

        /// <summary>
        /// 上一步Post
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        public ActionResult BackFlowProcedure(FormCollection form)
        {
            List<SelectListItem> typeList = new List<SelectListItem>();
            typeList.Add(new SelectListItem() { Text = "人员", Value = "人员" });
            typeList.Add(new SelectListItem() { Text = "职位", Value = "职位" });
            FlowProcedureView flowProcedureView = new FlowProcedureView(procedureBind, typeList);

            IFlowService fs = new FlowService();

            if (form["Step"].ToString() == "1")//如果是第一个步骤，则返回新建流程页面
            {
                foreach (KeyValuePair<string, List<object>> pair in flowDictionary)
                {
                    //if (pair.Key == User.Identity.Name)
                    if (pair.Key == "陈大捷")
                    {
                        flowBind = (Flow)pair.Value[0];//查找Dictionary数据，绑定页面
                    }
                }

                IList<SelectListItem> flowTypeList = fs.GetAllFlowTypeList();
                IList<SelectListItem> FormNameList = fs.GetAllFormNameList();

                foreach (KeyValuePair<string, List<object>> pair in flowDictionary)//绑定下拉列表
                {
                    //if (pair.Key == User.Identity.Name)
                    if (pair.Key == "陈大捷")
                    {
                        foreach (SelectListItem sli in flowTypeList)//流程类型
                        {
                            if (sli.Text == ((Flow)pair.Value[0]).Flowtype.Ft_name)
                            {
                                sli.Selected = true;
                            }
                        }
                        foreach (SelectListItem sli in FormNameList)//表单类型
                        {
                            if (sli.Text == ((Flow)pair.Value[0]).Form.Fo_name)
                            {
                                sli.Selected = true;
                            }
                        }
                    }
                }
                NewFlowView newFlowView = new NewFlowView(flowBind, flowTypeList, FormNameList);
                return View("NewFlow", newFlowView);
            }
            else//如果不是第一个步骤，则返回前一个步骤页面
            {
                ViewData["Step"] = Convert.ToInt32(form["Step"]) - 1;
                foreach (KeyValuePair<string, List<object>> pair in flowDictionary)
                {
                    //if (pair.Key == User.Identity.Name)
                    if (pair.Key == "陈大捷")
                    {
                        //如果步骤页面还没保存就点击上一页，则添加步骤
                        if (pair.Value.Count == Convert.ToInt32(form["Step"]))
                        {
                            Procedure updateProcedure = new Procedure();
                            updateProcedure.Step_id = Convert.ToInt32(form["Step"]);
                            updateProcedure.Pr_name = form["ProcedureName"];
                            updateProcedure.Pr_remark = form["ProcedureRemark"];
                            updateProcedure.Pr_type = form["ProcedureType"];
                            updateProcedure.Pr_typevalue = form["ProcedureTypeValue"];
                            updateProcedure.Pr_isfinished = false;
                            pair.Value.Add(updateProcedure);
                        }
                        else//如果步骤页面已经保存，则修改步骤
                        {
                            Procedure updateProcedure = new Procedure();
                            updateProcedure.Step_id = Convert.ToInt32(form["Step"]);
                            updateProcedure.Pr_name = form["ProcedureName"];
                            updateProcedure.Pr_remark = form["ProcedureRemark"];
                            updateProcedure.Pr_type = form["ProcedureType"];
                            updateProcedure.Pr_typevalue = form["ProcedureTypeValue"];
                            updateProcedure.Pr_isfinished = false;
                            pair.Value[Convert.ToInt32(form["Step"])] = updateProcedure;
                        }

                        procedureBind = (Procedure)pair.Value[Convert.ToInt32(form["Step"]) - 1];////查找上一步骤数据
                        foreach (SelectListItem sli in typeList)
                        {
                            if (procedureBind.Pr_type == sli.Text)//绑定相应的选定的下拉列表
                            {
                                sli.Selected = true;
                            }
                        }
                        flowProcedureView.typeList = typeList;
                        flowProcedureView.procedure = procedureBind;
                    }
                }
                return View("FlowProcedure", flowProcedureView);
            }
        }


        /// <summary>
        /// 新建流程完成
        /// </summary>
        public ActionResult NewFLowSuccess(FormCollection form)
        {
            IFlowService fs = new FlowService();

            Procedure procedure = new Procedure();
            foreach (KeyValuePair<string, List<object>> pair in flowDictionary)
            {
                //if (pair.Key == User.Identity.Name)
                if (pair.Key == "陈大捷")
                {
                    procedure.Flow = (Flow)pair.Value[0];//获取步骤类型
                }
            }
            procedure.Step_id = Convert.ToInt32(form["Step"]);  //
            procedure.Pr_name = form["ProcedureName"];
            procedure.Pr_remark = form["ProcedureRemark"];
            procedure.Pr_type = form["ProcedureType"];
            procedure.Pr_typevalue = form["ProcedureTypeValue"];
            procedure.Pr_isfinished = true;

            foreach (KeyValuePair<string, List<object>> pair in flowDictionary)
            {
                //if (pair.Key == User.Identity.Name)//查找key为登录名的dictionary
                if (pair.Key == "陈大捷")//查找key为登录名的dictionary
                {
                    pair.Value.Add(procedure);//添加对象
                    //全部对象保存
                    for (int i = 0; i < pair.Value.Count; i++)
                    {
                        if (i == 0)//第一个为Flow对象
                        {
                            ((Flow)pair.Value[0]).Save();
                        }
                        else//其余为Procedure对象
                        {
                            ((Procedure)pair.Value[i]).Save();
                        }
                    }
                }
            }
            //flowDictionary.Remove(User.Identity.Name);//根据用户名移除Dictionary记录
            flowDictionary.Remove("陈大捷");//根据用户名移除Dictionary记录

            return View();
        }

        /// <summary>
        /// 取消流程设计
        /// </summary>
        /// <returns></returns>
        public ActionResult NewFlowCancel()
        {
            foreach (KeyValuePair<string, List<object>> pair in flowDictionary)
            {
                //if (pair.Key == User.Identity.Name)//查找key为登录名的dictionary
                if (pair.Key == "陈大捷")//查找key为登录名的dictionary
                {
                    //flowDictionary.Remove(User.Identity.Name);//移除用户数据
                    flowDictionary.Remove("陈大捷");
                    break;
                }
            }

            return RedirectToAction("Index", "Home");//跳转到首页
        }

        //------------------------------------以下为新建表单-------------------------------------

        /// <summary>
        /// 新建表单
        /// </summary>
        public ActionResult NewForm()
        {
            IFlowService fs = new FlowService();
            FormData formData = new FormData();
            List<SelectListItem> typeList = fs.GetAllFlowTypeList();
            NewFormView model = new NewFormView(typeList, formData);
            return View(model);
        }

        /// <summary>
        /// 新建表单下一步Post
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult NewForm(FormCollection form)
        {
            IFlowService fs = new FlowService();

            FormData formData = new FormData();
            formData.Fo_name = form["FormName"];
            formData.Fo_remark = form["FormRemark"];
            formData.Flowtype = fs.GetFlowType(form["FlowType"]);

            foreach (KeyValuePair<string, FormData> pair in formDictionary)//判断是否存在用户名数据
            {
                if (pair.Key == User.Identity.Name)//查找key为登录名的dictionary
                {
                    formData.Fo_html = pair.Value.Fo_html;

                    formDictionary.Remove(User.Identity.Name);
                    formDictionary.Add(User.Identity.Name, formData);//加进字典

                    return View("DesignForm", formData);
                }
            }

            formDictionary.Add(User.Identity.Name, formData);//保存进formDictionary
            return View("DesignForm", formData);
        }

        /// <summary>
        /// 设计表单Post
        /// </summary>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult DesignForm(FormCollection form)
        {
            IFlowService fs = new FlowService();

            foreach (KeyValuePair<string, FormData> pair in formDictionary)
            {
                if (pair.Key == User.Identity.Name)//查找key为登录名的dictionary
                {
                    FormData f = (FormData)pair.Value;//根据id查找Form
                    f.Fo_html = form["Html"];
                    fs.Save(f);//保存
                }
            }
            return View("NewFormSuccess");
        }

        /// <summary>
        /// 设计表单上一步
        /// </summary>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult DesignFormBack(FormCollection form)
        {
            IFlowService fs = new FlowService();
            FormData formData = null;
            foreach (KeyValuePair<string, FormData> pair in formDictionary)
            {
                if (pair.Key == User.Identity.Name)//查找key为登录名的dictionary
                {
                    formData = pair.Value;//根据用户名查找Form
                    formData.Fo_html = form["Html"];
                    formDictionary.Remove(User.Identity.Name);
                    formDictionary.Add(User.Identity.Name, formData);//更新字典

                    break;
                }
            }

            List<SelectListItem> typeList = fs.GetAllFlowTypeList();
            foreach (SelectListItem sli in typeList)
            {
                if (formData.Flowtype.Ft_name == sli.Text)//绑定相应的选项到下拉列表
                {
                    sli.Selected = true;
                    break;
                }
            }
            NewFormView model = new NewFormView(typeList, formData);
            return View("NewForm",model);
        }

        /// <summary>
        /// 取消表单设计
        /// </summary>
        /// <returns></returns>
        public ActionResult NewFormCancel()
        {
            foreach (KeyValuePair<string, FormData> pair in formDictionary)
            {
                //if (pair.Key == User.Identity.Name)//查找key为登录名的dictionary
                if (pair.Key == User.Identity.Name)//查找key为登录名的dictionary
                {
                    //flowDictionary.Remove(User.Identity.Name);//移除用户数据
                    formDictionary.Remove(User.Identity.Name);
                    break;
                }
            }

            return RedirectToAction("Index", "Home");//跳转到首页
        }
    }
}
